import requests
import json
import re
headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
}
url="https://pvp.qq.com/web201605/js/herolist.json"
base_url="http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{}/{}-bigskin-{}.jpg"
detail_url = "https://pvp.qq.com/web201605/herodetail/{}.shtml"

# resp = requests.get(url,headers=headers)
# with open("./data/herolist.json","w",encoding="utf-8")as f:
#     f.write(resp.text)


with open("./data/herolist.json",'r',encoding="utf-8")as f:
    hero_list = json.loads(f.read())

ul_pattern = re.compile(r'<ul class="pic-pf-list pic-pf-list3" data-imgname="(.*?)">')

for hero in hero_list:
    hero_name = hero['cname']
    ename=hero['ename']
    detail_url_full = detail_url.format(ename)
    resp2 = requests.get(url = detail_url_full,headers=headers)
    pifu_list = ul_pattern.findall(resp2.content.decode("gbk"))[0].split('|')
    for i,skin in enumerate(pifu_list):
        skin_name = skin.partition('&')[0]
        url = base_url.format(ename,ename,i+1)
        resp3 = requests.get(url,headers=headers)
        with open(f"./img/{hero_name}-{skin_name}.jpg","wb")as f:
            f.write(resp3.content)
